﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Battle
{
    public class PVEBattleOver : IBattleOver
    {
        public BattleType GetBattleType()
        {
            return BattleType.PVE;
        }

        bool localResult;
        bool remoteResult;

        OnBattleResult onResult = null;
        public PVEBattleOver(OnBattleResult onResult)
        {
            this.onResult = onResult;

        }

        public void Excute()
        {
            localResult = BattleManager.Instance.battleViewCtrl.winCamp == BattleCamp.Attacker;
            remoteResult = false;

            AskCopyFightEnd();
        }

        void AskCopyFightEnd()
        {
            ProtoBuf.Message.CSCopyFightEnd msg = new ProtoBuf.Message.CSCopyFightEnd();
            msg.fightId = StaticData.curBattleData.fightId;
            var data = BattleManager.Instance.battleEngine.compute.data;
            StaticData.curBattleData.myCopyBattlePlayer.inst.anger = data.attacker.angerNum;
            foreach (var hero in StaticData.curBattleData.myCopyBattlePlayer.formation)
            {
                var actor = data.GetActorByUID("0_" + hero.guid);
                hero.inst.curHp = actor.attribute.hp;
                hero.pose = actor.MainPosition.ToFPID();
            }
            msg.battlePlayer = StaticData.curBattleData.myCopyBattlePlayer;
            msg.result = localResult ? 1 : 0;
            msg.optRecord = BattleNetOver.ParseOptRecord();
            NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CSCopyFightEnd, msg, OnAskCopyFightEndCallback);
        }

        void OnAskCopyFightEndCallback(NetWork.Packets.Packet pack, bool bSuccess)
        {
            if (bSuccess)
            {
                ProtoBuf.Message.SCCopyFightEnd _msg = pack.kBody as ProtoBuf.Message.SCCopyFightEnd;
                remoteResult = _msg.fightResult > 0 ? true : false;
                StaticData.lastBattleEndMsg = _msg;
                StaticData.isLastBattleWin = remoteResult;
                StaticData.isCompleteTask = _msg.isCopyComplete > 0 || !remoteResult;

                // 副本胜利通关，任务完成
                if (_msg.isCopyComplete > 0 && remoteResult)
                {
                    AskCopyMissionComplete(_msg);
                }
                else
                {
                    OnCopyBattleNetFinish(_msg);
                }
            }
        }

        void AskCopyMissionComplete(ProtoBuf.Message.SCCopyFightEnd msg)
        {
            Story.StoryManager.Instance.OnMissionComplete(StaticData.currFightLevelId, () => {
                OnCopyBattleNetFinish(msg);
            });

        }

        void OnCopyBattleNetFinish(ProtoBuf.Message.SCCopyFightEnd msg)
        {
            if (msg.isCopyComplete > 0 || !remoteResult)
            {
                UI.UICopyData.TryToSetLevelStar(StaticData.currFightLevelId, msg.star);
                StaticData.willBaseDrops.Clear();
                StaticData.isPveAutoCache = false;
            }
            else
            {
                StaticData.isPveAutoCache = BattleManager.Instance.battleViewCtrl.isAuto;
                StaticData.willBaseDrops.AddRange(msg.currentResource);
            }

            End();
        }


        public void End()
        {

            if (onResult != null)
            {
                onResult(remoteResult);
            }

        }

    }
}
